#include "ImageSetting.h"
#include<QJsonDocument>
#include<QJsonObject>
#include<QJsonArray>
#include<QFile>

namespace RuntimeData {
ImageSetting::ImageSetting() { }

ImageSetting *ImageSetting::getInstance()
{
    static ImageSetting singleton{};
    return &singleton;
}

void ImageSetting::loadImageSettingInfo()
{
    for (int i = 0; i < 6; i++) {
        __loadImageSettingInfo(i);
    }
}

void ImageSetting::__loadImageSettingInfo(int mirrorType)
{
    QString fileName = QString("config/imageSetting%1.json").arg(mirrorType);
    QFile file(fileName);
    QJsonObject obj;

    if (file.exists()) {
        iLog(QString("%1 file exists").arg(fileName));
        readImageSettingInfoByRom(mirrorType);
        return;
    }
    if (!file.open(QIODevice::ReadWrite)) {
        iLog(QString("%1 open error, use default bootSetting").arg(fileName));
        setDefalutImageSettingInfo(mirrorType);
        return;
    }
    setDefalutImageSettingInfo(mirrorType);
    for (int i=0;i<IMAGE_SETTING_ITEM_COUNT;++i) {
        auto index = QString("settingItem%1").arg(i);
        obj[index] = g_settingItemValue[mirrorType][i];
    }
    QJsonDocument jdoc(obj);
    file.write(jdoc.toJson());
    file.flush();
    file.close();
}

void ImageSetting::readImageSettingInfoByRom(int mirrorType)
{
    QString fileName = QString("config/imageSetting%1.json").arg(mirrorType);

    QFile file(fileName);
    QByteArray data;
    QJsonDocument doc;
    QJsonObject obj;

    if (!file.open(QIODevice::ReadOnly)) {
        iLog(QString("%1 open error, use default ImageSetting").arg(fileName));
        setDefalutImageSettingInfo(mirrorType);
        return;
    }
    data = file.readAll();
    file.close();

    doc = QJsonDocument::fromJson(data);
    if(doc.isObject()) {
        /* 把json文档转换为json对象 */
        obj = doc.object();
        for (int i=0;i<IMAGE_SETTING_ITEM_COUNT;++i) {
            auto index = QString("settingItem%1").arg(i);
            g_settingItemValue[mirrorType][i] = obj.value(index).toInt();
        }
    }
}

static int defalutSetting[6][IMAGE_SETTING_ITEM_COUNT] = {
    {4, 4, 4, 0, 0, 0, 0, 0, 5, 4, 0, 15,0,0,0},
    {0, 0, 0, 0, 0, 0, 0, 0, 5, 4, 0, 15,0,0,0},
    {0, 0, 0, 0, 0, 0, 0, 0, 5, 4, 0, 15,0,0,0},
    {0, 0, 0, 0, 0, 0, 0, 0, 5, 4, 0, 15,0,0,0},
    {0, 0, 0, 0, 0, 0, 0, 0, 5, 4, 0, 15,0,0,0},
    {0, 0, 0, 0, 0, 0, 0, 0, 5, 4, 0, 15,0,0,0},
};
void ImageSetting::setDefalutImageSettingInfo(int mirrorType)
{
    for(int i = 0;i<IMAGE_SETTING_ITEM_COUNT;++i) {
        g_settingItemValue[mirrorType][i] = defalutSetting[mirrorType][i];
    }
}

void ImageSetting::saveImageSettingToRom(int mirrorType)
{
    QString fileName = QString("config/imageSetting%1.json").arg(mirrorType);

    QFile file(fileName);
    QJsonObject obj;

    file.open(QIODevice::WriteOnly);

    for (int i=0;i<IMAGE_SETTING_ITEM_COUNT;++i) {
        auto index = QString("settingItem%1").arg(i);
        obj[index] = g_settingItemValue[mirrorType][i];
        qDebug()<<index<<"="<<g_settingItemValue[mirrorType][i];
    }
    QJsonDocument jdoc(obj);
    file.write(jdoc.toJson());
    file.flush();
    file.close();
}

void ImageSetting::recoverDefalutImageSettingInfo(int mirrorType)
{
    setDefalutImageSettingInfo(mirrorType);
    saveImageSettingToRom(mirrorType);
}
}
